package example.demo.nospring.plugins;

import cn.hutool.core.util.ReflectUtil;
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
import org.apache.ibatis.executor.statement.RoutingStatementHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;

import java.lang.reflect.Field;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.util.Properties;

@Intercepts(
        {
                @Signature(type = StatementHandler.class, method = "prepare",
                        args = {Connection.class, Integer.class})
        }
)
public class LimitPlugin implements Interceptor {
//    @Override
//    public Object intercept(Invocation invocation) throws Throwable {
//        /*true代表需要分页*/
////        Object[] args = invocation.getArgs();
////        if (args[args.length - 1] instanceof Boolean && (boolean) args[args.length - 1]) {
////
////        }
//        Object target = invocation.getTarget();
//        //如果这个是第二插件 这个 target instanceof RoutingStatementHandler 就是false
//        if (target instanceof StatementHandler) {
//            StatementHandler handler = (StatementHandler) target;
//            Field delegateField = ReflectUtil.getField(RoutingStatementHandler.class, "delegate");
//            delegateField.setAccessible(true);
//            StatementHandler statementHandler = null;
//            /*说明前面有plugin做了拦截*/
//            if (!(target instanceof RoutingStatementHandler)) {
//                System.out.println(handler);
//                if (target instanceof Proxy) {
//                    Proxy proxy = (Proxy) handler;
//                    System.out.println();
//                    Class<Proxy> proxyClass = Proxy.class;
//                    Field h = proxyClass.getDeclaredField("h");
//                    h.setAccessible(true);
//                    Object o = h.get(proxy);
//                    if (o instanceof Plugin){
//                        Plugin plugin= (Plugin) o;
//
//                    }
//                }
//
//
//            }
//            statementHandler = (StatementHandler) delegateField.get(handler);
//            BoundSql boundSql = statementHandler.getBoundSql();
//            String sql = boundSql.getSql();
//            System.out.println("boundSql.getSql()=====" + sql);
//        }
//        return invocation.proceed();
//    }

    @Override
    public Object intercept(Invocation invocation) throws Throwable {

        Object target = invocation.getTarget();
        //说明前面已经有了插件做了处理
        if (!(target instanceof RoutingStatementHandler)) {
            if (Proxy.isProxyClass(target.getClass())) {
                Object realTarget = realTarget(target);
                System.out.println();
            }
        }



        return invocation.proceed();
    }

    public static <T> T realTarget(Object target) {
        if (Proxy.isProxyClass(target.getClass())) {
            MetaObject metaObject = SystemMetaObject.forObject(target);
            return realTarget(metaObject.getValue("h.target"));
        }
        return (T) target;
    }
    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {

    }
}
